package boot

import (
	"dev.ixmxm.top/rms/utils/buffer"
	"dev.ixmxm.top/rms/utils/log"
)

func NewChannelEncodeWriter(level log.Level, ch chan log.Entry) log.EncodeWriter {
	writer := new(channelEncodeWriter)
	writer.level = level
	writer.ch = ch
	return writer
}

type channelEncodeWriter struct {
	level log.Level
	ch    chan log.Entry
}

func (v *channelEncodeWriter) Encoder(entry log.Entry) (*buffer.Buffer, error) {
	if v.level > entry.Level {
		return &buffer.Buffer{}, nil
	}
	v.ch <- entry
	return &buffer.Buffer{}, nil
}

func (v *channelEncodeWriter) Write(p []byte) (int, error) {
	return len(p), nil
}
